(function($){
	$.fn.buyListInit = function(){
		var $this = $(this);
		var selectRow = null;
		var selectIndex = null;
		//选择商品
		var selectDialog= $('#selectDialog',$this);
		//门店下拉框
		$('#shop',$this).combobox({
			data:TEA.getShopList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:150
		})
		//查询的商品种类下拉框
		$('#item',selectDialog).combobox({
			data:TEA.getProductItemList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:150
		})
		//查询
		$('#search',$this).click(function(){
			var queryContent = $('.queryContent',$this);
			var shopID =$('#shop',$this).combobox('getValue');
			var productID = $('#productID',queryContent).val();
			var beginDate = $('#beginDate',queryContent).val();
			var endDate = $('#endDate',queryContent).val();
			
			var content = {shopID:shopID,productids:productID,beginDate:beginDate,endDate:endDate};
			var url = 'buy!query.action';
			$('#buyList',$this).datagrid({
				url:url,
				queryParams:content,
				pageNumber:1
			});
		})
		//重置
		$('#reset',$this).click(function(){
			var queryContent = $('.queryContent',$this);
			$('#shop',$this).combobox('clear');
			$('#productID',queryContent).val('');
			$('#productName',queryContent).val('');
			$('#beginDate',queryContent).val('');
			$('#endDate',queryContent).val('');
		})
		//加载查询页面列表
		$('#buyList',$this).datagrid({
			singleSelect:true,
			fit:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			showFooter:true,
			columns:[[
				{field:'buyID',hidden:true},
				{field:'buyCode',title:'入库单号',width:100,align:"center"},
				{field:'supplierName',title:'供应商',width:200,align:"center"},
				{field:'buyDate',title:'入库日期',width:100,align:"center"},
				{field:'productName',title:'品名',width:100,align:"center"},
				{field:'productSize',title:'规格',width:100,align:"center"},
				{field:'unitName',title:'单位',width:100,align:"center"},
				{field:'quantity',title:'数量',width:100,align:"center"},
				{field:'price',title:'单价',width:100,align:"center"},
				{field:'amount',title:'金额',width:100,align:"center"},
				{field:'storeHouseName',title:'仓库',width:100,align:"center"},
				{field:'buyer',title:'经办人',width:100,align:"center"}
			]]
		});
		$('#productSearch',$this).click(function(){
			$(selectDialog).dialog('open');
		})
		var onSelect = function(){
			var row = $('#productList',selectDialog).datagrid('getSelected');
			if(row==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$('#productID',$this).val(row.productID);
			$('#productName',$this).val(row.productName);
			onSelectExit();
		}
		
		var onSelectExit = function(){
			$(selectDialog).dialog('close');
		}
		
		$(selectDialog).dialog({  
		    title: '选择商品',  
		    width:900,
		    height:550,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'选择',iconCls:'icon-ok',handler:function(){onSelect()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onSelectExit()}}]
		});
		//选择页面查询
		$('#selectSearch',selectDialog).click(function(){
			var productItemID = $('#item',selectDialog).combobox('getValue');
			var productCode = $('#code',selectDialog).val();
			var productName = $('#name',selectDialog).val();
			
			var content = {productCode:productCode,productName:productName,productItemID:productItemID};
			
			$('#productList',selectDialog).datagrid({
				url:"product!select.action",
				queryParams:content,
				pageNumber:1
			});
		})
		//加载商品列表
		$('#productList',selectDialog).datagrid({
			singleSelect:true,
			fitColumns:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			height:450,
			width:880,
			columns:[[
				{field:'productID',hidden:true},
				{field:'productCode',title:'商品编号',width:100,align:"center"},
				{field:'productName',title:'商品名称',width:200,align:"center"},
				{field:'productSize',title:'规格',width:100,align:"center"},
				{field:'productPrice',title:'单价',width:100,align:"center"},
				{field:'unitName',title:'单位',width:100,align:"center"}
			]],
			onDblClickRow:function(rowIndex,rowData){
				onSelect();
			}
		});
	}
})(jQuery);